# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: Brian Galey <bkgaley at gmail dot com>
# Contributor: Pietro Zambelli <peter.zamb at gmail dot com>

_realname=libspatialite
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_rel=.a
_ver=4.3.0
pkgver=${_ver}${_rel}
pkgrel=4
pkgdesc="SQLite extension to support spatial data types and operations (mingw-w64)"
arch=('any')
url="https://www.gaia-gis.it/fossil/libspatialite/index"
license=('MPL' 'GPL' 'LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "automake" "autoconf")
depends=("${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
options=('strip')
source=("https://www.gaia-gis.it/gaia-sins/${_realname}-${_ver}${_rel//./}.tar.gz"
        "01-fix-pkgconfig.patch")
sha256sums=('88900030a4762904a7880273f292e5e8ca6b15b7c6c3fb88ffa9e67ee8a5a499'
            '50dace4542a2f378131be76c9dfe45fd78e7cf135f1339df8665b5f26c8ad6cc')

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${_rel//./}"
  sed -i configure.ac -e "s|mingw32|${MINGW_CHOST}|g"
  patch -p1 -i ${srcdir}/01-fix-pkgconfig.patch
  autoreconf
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  
  CPPFLAGS+=" -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H=1"
  
  ../${_realname}-${_ver}${_rel//./}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-libxml2

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
